const https = require('https')
const cheerio = require('cheerio')
const fs = require('fs')


https.get("https://movie.douban.com/top250", (res) => {
    let html = ''
    res.on('data', (data) => {
        html += data
    });
    res.on('end', () => {

        let film = []
        const $ = cheerio.load(html)
        $('li .item').each(function () {
            const title = $('.hd .title', this).text()
            const star = $('.bd .rating_num', this).text()
            const img = $('.pic img',this).attr('src')
            film.push({ title,star,img })
        })
        fs.writeFile('res.json',JSON.stringify(film),(err)=>{
            if(err) throw err
            console.log('File write sucess!');
        })
    })
})
